<?php

/*
Zoo Management Simulation
Displays the cage the user has selected from the Admin UI.
The file also toggles lighting for the cage and secures all doors for the cage
The user can add cage entries which is handled by the addNumberofEntries file and edit the cage through the cageProcess file
*/

include("dbconnect.php");

if ($_REQUEST['cageid']){
	$cagesql = "SELECT * FROM cage WHERE Cage_Id = '$_GET[cageid]'";
	$entrysql = "SELECT * FROM entry WHERE Cage_Id = '$_GET[cageid]'";
	$cagenumber = $_GET['cagenumber'];
} else
{echo "Error";
}

$cageresult = mysql_query($cagesql);
$entryresult = mysql_query($entrysql);
	while(($cagerow = mysql_fetch_row($cageresult)) !== FALSE)
	{
		$id = $cagerow[0];
		$name = $cagerow[1];
		$length = $cagerow[2];
		$width = $cagerow[3];
		$height = $cagerow[4];
		$exhibitname = $cagerow[6];
		$exhibitdesc = $cagerow [7];
		$humandetector = $cagerow[8];
		$typeresult = mysql_query("SELECT * FROM type WHERE Type_Id = ".$cagerow[5]);
		while(($typerow = mysql_fetch_row($typeresult)) !== FALSE)
		{
			$type = $typerow[1];
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zoo Management Cage Page</title>
<link href="mainstyle.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Zoo Management Cage Page</h1>

<table id="zootable" width="100%" border="1">
  <tr>
    <td id="tableheader">Name</td>
    <td id="tableheader">Enclosure Number</td>
    <td id="tableheader">Length</td>
    <td id="tableheader">Width</td>
    <td id="tableheader">Height</td>
    <td id="tableheader">Type</td>
    <td id="tableheader">Number of entries</td>
    <td id="tableheader">Exhibit Name</td>
    <td id="tableheader">Exhibit Description</td>
    <td id="tableheader">Human Detector</td>
  </tr>
  
	<?php
		echo "<tr>";
		echo "<td>". $name. "</td>";
		echo "<td>". $cagenumber . "</td>";
		echo "<td>". $length. "m</td>";
		echo "<td>". $width. "m</td>";
		echo "<td>". $height. "m</td>";
		echo "<td>". $type. "</td>";
		$countresult = mysql_query("SELECT Cage_Id FROM entry WHERE Cage_Id='". $id . "'");
		$numberofrows = mysql_num_rows($countresult);
		echo "<td>". $numberofrows . "</td>";
		echo "<td>". $exhibitname. "</td>";
		echo "<td>". $exhibitdesc. "</td>";
		echo "<td>". $humandetector. "</td>";
		echo "</tr>";
	?> 
</table>
<br />		

<table id="zootable" width="100%" border="1">
	  <tr>
	    <td id="tableheader">Entry</td>
	    <td id="tableheader">Door Type</td>
	    <td id="tableheader">Door Status</td>
      </tr>
        <?php
			$entrytableresult = mysql_query($entrysql);
			$entrynumber = 1;
			while(($entrytablerow = mysql_fetch_row($entrytableresult)) !== FALSE)
			{
				echo "<tr>";
				echo "<td>". $entrynumber . "</td>";
				echo "<td>". $entrytablerow[2] . "</td>";
				echo "<td>". $entrytablerow[3] . "</td>";
				echo "</tr>";
				$entrynumber+=1;
			}
		?>
        </table>
        <form id="addCageEntryForm" name="addCageEntryForm" method="post" enctype="multipart/form-data" action="addNumberofEntries.php">
        <?php
		echo "<input type='hidden' name='cageid' value='".$id . "' />";	
		?>
        <input type="submit" name="submit" id="submit" value="Add Cage Entries" /></form>

<?php
$lighting = $_REQUEST['lighting'];
if ($lighting == 1){
	echo "Lighting enabled";
}
else if ($lighting == 0){
	echo "Lighting disabled";
}
?>

<p>
<form id="cageForm" name="cageForm" method="post" enctype="multipart/form-data" action="cageProcess.php">
<?php
echo "<input type='hidden' name='cageid' value='".$id . "' />";
echo "<input type='hidden' name='cagenumber' value='".$cagenumber . "' />";
echo "<input type='hidden' name='type' value='".$type . "' />";
echo "<input type='hidden' name='lighting' value='".$lighting . "' />";	
?>
<input type="submit" name="submit" id="submit" value="Edit Cage" />
<input type="submit" name="submit" id="submit" value="Secure doors" />
<input type="submit" name="submit" id="submit" value="Toggle lighting" />
<input type="submit" name="submit" id="submit" value="Return to Administator page" />
</form>
</p>
</body>
</html>